<template>
  <div class="components-prod-manage">
    <div class="search-bar">
      <el-form
        ref="searchFormRef"
        :inline="true"
        class="search-form"
        :model="searchForm"
        @submit.prevent
      >
        <div class="input-row">
          <el-form-item
            prop="prodName"
            :label="$t('coupon.productName') + ':'"
          >
            <el-input
              v-model="searchForm.prodName"
              type="text"
              clearable
              :placeholder="$t('coupon.productName')"
            />
          </el-form-item>
          <el-form-item
            prop="state"
            :label="$t('shop.status') + ':'"
          >
            <el-select
              v-model="searchForm.state"
              clearable
              :placeholder="$t('shop.status')"
            >
              <el-option
                value="1"
                :label="$t('prodList.onShelf')"
              />
              <el-option
                value="0"
                :label="$t('prodList.offShelf')"
              />
            </el-select>
          </el-form-item>
          <el-form-item>
            <div
              class="default-btn primary-btn"
              @click="onSearch(true)"
            >
              {{ $t('crud.searchBtn') }}
            </div>
            <div
              class="default-btn"
              @click="resetForm('searchForm')"
            >
              {{ $t('crud.emptyBtn') }}
            </div>
          </el-form-item>
        </div>
      </el-form>
    </div>
    <div class="main-container">
      <div class="operation-bar">
        <div
          v-if="isAuth('distribution:distributionProd:save')"
          class="default-btn primary-btn"
          @click.stop="onAddOrUpdate()"
        >
          {{ $t('crud.addTitle') }}
        </div>

        <div
          v-if="isAuth('distribution:distributionProd:delete')"
          :class="[dataListSelections.length <= 0 ? 'disabled-btn' : '', 'default-btn']"
          :disabled="dataListSelections.length <= 0"
          @click.stop="onDelete"
        >
          {{ $t('sys.batchDelete') }}
        </div>
      </div>
      <div class="table-con">
        <el-table
          ref="prodBasicListTableRef"
          :data="dataList"
          header-cell-class-name="table-header"
          row-class-name="table-row"
          style="width: 100%"
          @selection-change="onSelectionChange"
        >
          <el-table-column
            type="selection"
            width="55"
          />
          <!-- 商品信息 -->
          <el-table-column
            align="left"
            prop="prodName"
            :label="$t('product.prodInfo')"
            min-width="200"
          >
            <template #default="scope">
              <div class="table-cell-con">
                <div class="table-cell-image">
                  <ImgShow :src="scope.row.product.pic" />
                </div>
                <span class="table-cell-text">{{ scope.row.product.prodName }}</span>
              </div>
            </template>
          </el-table-column>
          <el-table-column
            :label="$t('shop.status')"
            prop="state"
          >
            <template #default="scope">
              <span v-if="scope.row.state === 0">{{ $t('prodList.offShelf') }}</span>
              <span v-else>{{ $t('prodList.onShelf') }}</span>
            </template>
          </el-table-column>
          <!-- 奖励比例 -->
          <el-table-column
            prop="state"
            :label="$t('marketing.rewardRatio')"
          >
            <template #default="scope">
              <div
                v-if="scope.row.awardProportion === 0"
                class="tag-text"
              >
                {{ $t('marketing.proporteward') }}
              </div>
              <div
                v-if="scope.row.awardProportion === 1"
                class="tag-text"
              >
                {{ $t('marketing.rewardByFixedValue') }}
              </div>
            </template>
          </el-table-column>
          <!-- 间推奖励 -->
          <el-table-column
            prop="state"
            :label="$t('marketing.inviterReward')"
          >
            <template #default="scope">
              <div
                v-if="scope.row.parentAwardSet === 0"
                class="tag-text"
              >
                {{ $t('seckill.close') }}
              </div>
              <div
                v-if="scope.row.parentAwardSet === 1"
                class="tag-text"
              >
                {{ $t('seckill.open') }}
              </div>
            </template>
          </el-table-column>
          <el-table-column
            align="center"
            :label="$t('crud.menu')"
            width="250"
          >
            <template #default="scope">
              <div class="text-btn-con">
                <div
                  v-if="isAuth('distribution:distributionProd:update')"
                  class="default-btn text-btn"
                  @click="onAddOrUpdate(scope.row)"
                >
                  {{ $t('crud.updateBtn') }}
                </div>
                <div
                  v-if="isAuth('prod:prod:delete')"
                  class="default-btn text-btn"
                  @click="onDelete(scope.row)"
                >
                  {{ $t('text.delBtn') }}
                </div>
              </div>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <el-pagination
        :current-page="page.currentPage"
        :page-sizes="[10, 20, 50, 100]"
        :page-size="page.pageSize"
        :total="page.total"
        layout="total, sizes, prev, pager, next, jumper"
        @update:page-size="onPageSizeChange"
        @update:current-page="onPageChange"
      />
    </div>
    <!-- 弹窗, 新增 / 修改 -->
    <ProdAddOrUpdate
      v-if="prodAddOrUpdateVisible"
      ref="prodAddOrUpdateRef"
      @refresh-data-list="refreshChange"
    />
  </div>
</template>

<script setup>
import ProdAddOrUpdate from './prod-add-or-update.vue'
import { isAuth } from '@/utils/index'
import { ElMessage, ElMessageBox } from 'element-plus'

let tempSearchForm = null // 保存上次点击查询的请求条件

const searchFormRef = ref(null)
const page = reactive({
  total: 0, // 总页数
  currentPage: 1, // 当前页数
  pageSize: 10 // 每页显示多少条
})
const searchForm = reactive({
  prodName: '',
  state: ''
})
const dataList = ref([])
const dataListSelections = ref([])
const prodAddOrUpdateVisible = ref(false)
onMounted(() => {
  getDataList()
})

// 多选回调
const onSelectionChange = list => {
  dataListSelections.value = list
}
// 新增 / 修改
const prodAddOrUpdateRef = ref(null)
const onAddOrUpdate = data => {
  prodAddOrUpdateVisible.value = true
  nextTick(() => {
    prodAddOrUpdateRef.value?.init(data)
  })
}
// 点击查询
const onSearch = (newData = false) => {
  page.currentPage = 1
  getDataList(page, newData)
}
// 刷新回调
const refreshChange = () => {
  getDataList(page)
}
// 获取数据列表
const getDataList = (pageParam, newData = false) => {
  if (newData || !tempSearchForm) {
    tempSearchForm = JSON.parse(JSON.stringify(searchForm))
  }
  http({
    url: http.adornUrl('/distribution/distributionProd/page'),
    method: 'get',
    params: http.adornParams(
      Object.assign(
        {
          current: pageParam ? pageParam.currentPage : 1,
          size: pageParam ? pageParam.pageSize : 10
        },
        tempSearchForm
      )
    )
  }).then(({ data }) => {
    page.total = data.total
    page.pageSize = data.size
    page.currentPage = data.current
    dataList.value = data.records
  })
}
// 删除
const onDelete = row => {
  const ids = row.distributionProdId ? [row.distributionProdId] : dataListSelections.value.map(item => {
    return item.distributionProdId
  })
  if (!ids.length) {
    return
  }
  ElMessageBox.confirm(
    `${$t('sys.makeSure')}${row.distributionProdId ? $t('text.delBtn') : $t('sys.batchDelete')}${$t(
      'text.menu'
    )}?`,
    $t('text.tips'),
    {
      confirmButtonText: $t('crud.filter.submitBtn'),
      cancelButtonText: $t('crud.filter.cancelBtn'),
      type: 'warning'
    }
  ).then(() => {
    http({
      url: http.adornUrl('/distribution/distributionProd'),
      method: 'delete',
      data: http.adornData(ids, false)
    }).then(() => {
      ElMessage({
        message: $t('publics.operation'),
        type: 'success',
        duration: 1500,
        onClose: () => {
          refreshChange()
        }
      })
    })
  })
}
const resetForm = () => {
  searchFormRef.value.resetFields()
}
const onPageSizeChange = val => {
  page.pageSize = val
  getDataList(page)
}
const onPageChange = val => {
  page.currentPage = val
  getDataList(page)
}
</script>
